<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Article_model extends Model
{
	function Article_model()
	{
		parent::Model();
	}
	function add_record($f_path)
	{
		$data=array(
			'q_id'=>$this->input->post("q_id"),
			'title'=>$this->input->post("title"),
			'summary'=>$this->input->post("summary"),
			'content'=>$this->input->post("content")
		);
		if($f_path['file_path'])
		{
			$data['pic_path']=$f_path['file_path'];
		}
		else
		{
			$data['pic_path']=$this->input->post("pic_path");
		}
		$this->db->insert("article",$data);
		return TRUE;
	}
	function get_records($per_page=10)
	{
		$this->load->library('pagination');
		$total_rows = $this->db->count_all_results('article');
		$this->db->order_by('id','desc');
		$offset = $this->uri->segment(4, 0);
		if($total_rows==$offset||$total_rows<$offset)
		{
			$offset=0;
		}
		$this->pagination->initialize(
			array(
					'base_url'		 => CTL_FOLDER.'article/index',
					'total_rows'	 => $total_rows,
					'per_page'		 => $per_page,
					'cur_page'       => $offset
			)
		);
		$data=array(
			'query'    => $this->db->get("article", $per_page, $offset),
			'paginate' => $this->pagination->create_links()
		);
		return $data;
	}
	
	function get_record($id)
	{
		$query=$this->db->get_where("article",array('id'=>$id));
		if($query->num_rows()>0)
		{
			return $query->row_array();
		}
	}
	
	function save_record($f_path)
	{
		$rd_id=$this->input->post("rd_id");
		$data=array(
			'q_id'=>$this->input->post("q_id"),
			'title'=>$this->input->post("title"),
			'summary'=>$this->input->post("summary"),
			'content'=>$this->input->post("content"),
		);
		if($f_path['file_path'])
		{
			$data['pic_path']=$f_path['file_path'];
		}
		else
		{
			$data['pic_path']=$this->input->post("pic_path");
		}
		$this->db->update("article",$data,array('id'=>$rd_id));
		return TRUE;
	}
	
	function del_record()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$this->db->where_in("id",$rd_id);
		$this->db->delete("article");
		if($this->db->affected_rows())
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}


}
?>